﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Web;
using System.Text.RegularExpressions;


namespace DateExtract
{
    public partial class Form1 : Form
    {

        #region private


        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        WebCrawler w;
        private void chkTitle_CheckedChanged(object sender, EventArgs e)
        {
            txtRet.Text = "Processing...........................................";
            w = new WebCrawler(txtWeb.Text);
            w.DownloadWebsite(txtURL.Text.Trim());
            Thread.Sleep(20000);
            txtRet.Text = w.Title;

        }

        #region Test Date
        private void chkDate_CheckedChanged(object sender, EventArgs e)
        {
            txtRet.Text = "Processing.............................................";
            w = new WebCrawler(txtWeb.Text);
            w.DownloadWebsite(txtURL.Text.Trim());
            Thread.Sleep(20000);
            txtRet.Text = w.Date;

        }

        #endregion
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtWeb.Text = openFileDialog.FileName;
            }
        }

        private void chkLead_CheckedChanged(object sender, EventArgs e)
        {
            txtRet.Text = "Processing.........................................";
            w = new WebCrawler(txtWeb.Text);
            w.DownloadWebsite(txtURL.Text.Trim());
            Thread.Sleep(20000);
            txtRet.Text = iso8859ToUnicode(w.Lead);
        }

        private void chkHtml_CheckedChanged(object sender, EventArgs e)
        {
            txtRet.Text = "Processing...........................................";
            w = new WebCrawler(txtWeb.Text);
            w.DownloadWebsite(txtURL.Text.Trim());
            Thread.Sleep(20000);
            if (!string.IsNullOrEmpty(w.Content))
                txtRet.Text = w.Content;
            else txtRet.Text = w.RawContent;
        }   

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            txtRet.Text = "Processing...........................................";
            w = new WebCrawler(txtWeb.Text);
            w.DownloadWebsite(txtURL.Text.Trim());
            Thread.Sleep(20000);
            if (!string.IsNullOrEmpty(w.html))
                txtRet.Text = w.html;
            else txtRet.Text = w.RawContent;
        }

        private void chkTxt_CheckedChanged(object sender, EventArgs e)
        {
            txtRet.Text = "Processing...........................................";
            w = new WebCrawler(txtWeb.Text);
            w.DownloadWebsite(txtURL.Text.Trim());
            Thread.Sleep(20000);
            if (!string.IsNullOrEmpty(w.Txt))
                txtRet.Text = w.Txt;
            else txtRet.Text = iso8859ToUnicode(w.RawContent);


        }
        /// <summary>
        /// Get all tat cả các thông tin
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            //txtRet.Text = "Processing...........................................";
            //txtRet.Text = "av ".Replace('', ' ').ToString();

          
            w = new WebCrawler(txtWeb.Text);
            w.DownloadWebsite(txtURL.Text.Trim());
            Thread.Sleep(3000);

            txtNgayThang.Text = w.Date;

            // txtTieude.Text = StripHTML(w.Title.Replace('','"'));



            //  txtTomtat.Text = StripHTML(w.Lead);
            //  txtNoidung.Text = w.Content.Replace('', '"'); ;
            // txtRet.Text = w.RawContent;
            // string s = StripHTML(txtTieude.Text);

            // s = iso8859ToUnicode(s);
            Thread.Sleep(2000);
            //test/?mod=detnews&catid=681&p=&id=247710
            txtTieude.Text = HttpUtility.HtmlDecode(w.Title);
            txtTomtat.Text = HttpUtility.HtmlDecode(w.Lead);
            txtNoidung.Text = HttpUtility.HtmlDecode(w.Content);
            txtRet.Text = HttpUtility.HtmlDecode(w.RawContent);


            //txtTieude.Text = HttpUtility.HtmlDecode(w.Title);
            //txtTomtat.Text = HttpUtility.HtmlDecode(w.Lead);
            //txtNoidung.Text = HttpUtility.HtmlDecode(w.Content);
            //txtRet.Text = HttpUtility.HtmlDecode(w.RawContent);
        }

        //private string StripHTML(string htmlString)
        //{
        //    string pattern = @"<(.|\n)*?>";
        //    return Regex.Replace(htmlString, pattern, string.Empty);

        //}

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            txtRet.Text = "Processing...........................................";
            w = new WebCrawler(txtWeb.Text);
            w.DownloadWebsite(txtURL.Text.Trim());
            Thread.Sleep(30000);

            txtTextTag.Text = w.Title + "." + w.Lead + "." + StripHTML(w.Content);
            try
            {
                testTag.HelloService fun = new testTag.HelloService();
                txtResultTag.Text = fun.getTagNPL(txtTextTag.Text, 4);
            }
            catch (Exception ex)
            {
                txtResultTag.Text = ex.StackTrace;
            }
        }
        private string StripHTML(string htmlString)
        {
            string pattern = @"<(.|\n)*?>";
            return Regex.Replace(htmlString, pattern, string.Empty);

        }
        /// <summary>
        /// Công ty C&#7893; ph&#7847;n &#273;&#7847;u t&#432; và du l&#7883;ch sinh thái R&#7891;ng Vi&#7879;t cùng Ban qu&#7843;n lý V&#432;&#7901;n chim B&#7841;c Liêu (S&#7903; NN&PTNT B&#7841;c Liêu) v&#7915;a khai tr&#432;&#417;ng khu du l&#7883;ch sinh thái V&#432;&#7901;n chim B&#7841;c Liêu  R&#7891;ng Vi&#7879;t vào ngày 18-2 t&#7841;i TP. B&#7841;c Liêu.
        /// 
        /// ASCIIEncoding ascii = new ASCIIEncoding();
      //  UTF8Encoding utf8 = new UTF8Encoding();
       // byte[] asciiBytes = Encoding.Convert(utf8, ascii, utf8bytes);
        /// </summary>
        /// <param name="textToConvert"></param>
        /// <returns></returns>
        public  string iso8859ToUnicode(string textToConvert)
        {
            Encoding iso8859 = Encoding.GetEncoding("iso-8859-1");
            Encoding unicode = Encoding.Unicode;
            byte[] srcTextBytes = iso8859.GetBytes(textToConvert);
           
            byte[] destTextBytes = Encoding.Convert(iso8859, unicode, srcTextBytes);

            char[] destChars = new char[unicode.GetCharCount(destTextBytes, 0, destTextBytes.Length)];
            unicode.GetChars(destTextBytes, 0, destTextBytes.Length, destChars, 0);

            StringBuilder result = new StringBuilder(textToConvert.Length + (int)(textToConvert.Length * 0.1));

            foreach (char c in destChars)
            {
                int value = Convert.ToInt32(c);
                if (value > 127)
                    result.AppendFormat("&#{0};", value);
                else
                    result.Append(c);
            }
            //http://www.congan.com.vn/?mod=detnews&catid=703&id=249338
            //&#147; là ký tự "
            //&#148; "
            string stemp= result.ToString().Replace("&#147;","''");
            stemp = stemp.ToString().Replace("&#148;", "''");


            //return result.ToString();
            return stemp;
        }

    }
}
